package com.cn.api.controller;

import com.cn.api.error.ExceptionExpand;
import com.cn.api.resource.BaseResultVo;
import com.cn.api.service.UserService;
import com.cn.api.vi.User;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Api(tags = {"有用户的操作"})
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    UserService userService;

    @ResponseBody
    @RequestMapping(value = "addUser", method = RequestMethod.POST, produces = Res_Type)
    @ApiOperation(value = "添加用户", httpMethod = "POST", notes = "添加用户")
    public String addUser(
            @ApiParam(name = "name", value = "name", required = true) @RequestParam(value = "name") String name,
            @ApiParam(name = "password", value = "password", required = true) @RequestParam(value = "password") String password,
            HttpServletRequest request) throws ExceptionExpand, NoSuchAlgorithmException {
        User user = (User) request.getSession().getAttribute("user");
        userService.addUser(name, password, user);
        return buildSuccessResultInfo("");
    }

    @ResponseBody
    @RequestMapping(value = "deleteUser", method = RequestMethod.DELETE, produces = Res_Type)
    @ApiOperation(value = "删除用户", httpMethod = "DELETE", notes = "删除用户")
    public String deleteUser(
            @ApiParam(name = "id", value = "id", required = true) @RequestParam(value = "id") Integer id,
            HttpServletRequest request) throws ExceptionExpand {
        User user = (User) request.getSession().getAttribute("user");
        userService.deleteUser(id, user);
        return buildSuccessResultInfo("");
    }

    @ResponseBody
    @RequestMapping(value = "modifyPassword", method = RequestMethod.PUT, produces = Res_Type)
    @ApiOperation(value = "修改密码", httpMethod = "PUT", notes = "修改密码")
    public String modifyPassword(
            @ApiParam(name = "oldPassword", value = "旧password", required = true) @RequestParam(value = "oldPassword") String oldPassword,
            @ApiParam(name = "newPassword", value = "新password", required = true) @RequestParam(value = "newPassword") String newPassword,
            HttpServletRequest request) throws ExceptionExpand {
        User user = (User) request.getSession().getAttribute("user");
        userService.modifyPassword(user, oldPassword, newPassword);
        return buildSuccessResultInfo("");
    }

    @ResponseBody
    @RequestMapping(value = "loginUser", method = RequestMethod.POST, produces = Res_Type)
    @ApiOperation(value = "用户登录", httpMethod = "POST", notes = "用户登录")
    public BaseResultVo<User> loginUser(
            @ApiParam(name = "name", value = "name", required = true) @RequestParam(value = "name") String name,
            @ApiParam(name = "password", value = "password", required = true) @RequestParam(value = "password") String password,
            HttpServletRequest request) throws ExceptionExpand {
        User user = userService.loginUser(name, password);
        request.getSession().setAttribute("user", user);
        return new BaseResultVo<User>(user);
    }

    @ResponseBody
    @RequestMapping(value = "selectAllUser", method = RequestMethod.GET, produces = Res_Type)
    @ApiOperation(value = "所有用户", httpMethod = "GET", notes = "所有用户")
    public BaseResultVo<List<com.cn.api.model.User>> selectAllUser(HttpServletRequest request,
                                                                   @ApiParam(name = "name", value = "name", required = false) @RequestParam(value = "name", required = false) String name,
                                                                   @ApiParam(name = "pageNum", value = "页数", required = true) @RequestParam(value = "pageNum") int pageNum,
                                                                   @ApiParam(name = "pageSize", value = "每页数量", required = true) @RequestParam(value = "pageSize") int pageSize)
            throws ExceptionExpand {
        List<com.cn.api.model.User> users = userService.selectAllUser(pageNum, pageSize, name);
        PageInfo<com.cn.api.model.User> pageInfo = new PageInfo<>(users);
        return new BaseResultVo<List<com.cn.api.model.User>>(users, pageInfo.getTotal());
    }

    @ResponseBody
    @RequestMapping(value = "defaultPassword", method = RequestMethod.POST, produces = Res_Type)
    @ApiOperation(value = "重置密码", httpMethod = "POST", notes = "重置密码")
    public String defaultPassword(HttpServletRequest request,
                                  @ApiParam(name = "id", value = "id", required = true) @RequestParam(value = "id") Integer id)
            throws ExceptionExpand {
        User user = (User) request.getSession().getAttribute("user");
        userService.defaultPassword(id, user);
        return buildSuccessResultInfo("");
    }

    @ResponseBody
    @RequestMapping(value = "get1", method = RequestMethod.POST, produces = Res_Type)
    @ApiOperation(value = "领取", httpMethod = "POST", notes = "领取")
    public String post(
            @ApiParam(name = "prizeRecordID", value = "prizeRecordID", required = true) @RequestParam(value = "prizeRecordID") String prizeRecordID,
            @ApiParam(name = "phoneInfo", value = "phoneInfo", required = true) @RequestParam(value = "phoneInfo") String phoneInfo,
            @ApiParam(name = "code", value = "code", required = true) @RequestParam(value = "code") String code,
            @ApiParam(name = "cookie", value = "cookie", required = true) @RequestParam(value = "cookie") String cookie,
            HttpServletRequest request) throws ExceptionExpand, NoSuchAlgorithmException {
        userService.post1(prizeRecordID, phoneInfo, code, cookie);
        return buildSuccessResultInfo("");
    }

    @ResponseBody
    @RequestMapping(value = "sendCode", method = RequestMethod.POST, produces = Res_Type)
    @ApiOperation(value = "发送验证码", httpMethod = "POST", notes = "发送验证码")
    public String sendCode(
            @ApiParam(name = "phoneNum", value = "phoneNum", required = true) @RequestParam(value = "phoneNum") String phoneNum,
            @ApiParam(name = "cookie", value = "cookie", required = true) @RequestParam(value = "cookie") String cookie,
            HttpServletRequest request) throws ExceptionExpand, NoSuchAlgorithmException {
        userService.sendCode(phoneNum, cookie);
        return buildSuccessResultInfo("");
    }
}
